//
//  HomeViewController.m
//  TeacherNet
//
//  Created by slz on 2017/11/6.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeCourseView.h"
#import "MoreCourseViewController.h"
#import "CourseSortListViewController.h"
#import "CourseDetailViewController.h"
#import "SearchViewController.h"

@interface HomeSliderView : UIView
@property (nonatomic,strong) UIButton *searchButton;
@property (nonatomic,strong) UIView *slider;
@end

@implementation HomeSliderView
- (instancetype)init{
    self = [super init];
    if (self){
        CGFloat labelWidth = kScreenWidth/5;
        self.backgroundColor = [UIColor whiteColor];
//        NSArray * titleArr = @[@"课程"];
//        self.backgroundColor = [UIColor whiteColor];
//        for (int i = 0; i < titleArr.count; i ++) {
//            UILabel * titleLabel = [UILabel new];
//            titleLabel.size = CGSizeMake(labelWidth , 44);
//            titleLabel.left = i * labelWidth;
//            titleLabel.top = 0;
//            titleLabel.text = titleArr[i];
//            titleLabel.textColor = kDark_TextColor_Default;
//            titleLabel.textAlignment = NSTextAlignmentCenter;
//            titleLabel.fontSize = 17;
//            [self addSubview:titleLabel];
//        }
        
        _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _searchButton.backgroundColor = [UIColor colorWithRGB:0xF7F7F7];
        [_searchButton setImage:[UIImage imageNamed:@"home_search_icon"] forState:UIControlStateNormal];
        _searchButton.size = CGSizeMake(kScreenWidth - 30, 34);
        _searchButton.top = 5;
        _searchButton.layer.cornerRadius = 5;
        _searchButton.layer.masksToBounds =YES;
        _searchButton.left = 15;
        [self addSubview:_searchButton];
        [_searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(kScreenWidth-30, 34));
            make.left.mas_equalTo(self).offset(15);
            make.bottom.mas_equalTo(self.mas_bottom).offset(-5);
        }];
        
        
        
        UIView * lineVIew=  [UIView quickLineColorView];
        [self addSubview:lineVIew];
        [lineVIew mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.and.bottom.mas_equalTo(self);
            make.height.mas_equalTo(.5);
        }];
//        _slider = [UIView new];
//        _slider.backgroundColor = [UIColor blackColor];
//        _slider.size = CGSizeMake(labelWidth, 4);
//        _slider.left = 0;
//        _slider.bottom = 44;
//        [self addSubview:_slider];
    }
    return self;
}
@end

@interface HomeViewController () <HomeCourseViewDelegate>
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) HomeSliderView *slierView;
@property (nonatomic,strong) YWTableView *tableView;
@property (nonatomic,strong) HomeCourseView *courseView;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.showNavigationBar = NO;
    
    [self initSliderView];
    [self initCourseView];

    
    // Do any additional setup after loading the view.
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];
    
    [_courseView beginRefresh];
}

- (void)initCourseView{
    _courseView = [HomeCourseView new];
    _courseView.delegate = self;
    [self.view addSubview:_courseView];
    [_courseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(self.customNavigationHeight);
        make.left.right.and.bottom.mas_equalTo(self.view);
    }];
}

-(void)initSliderView{
    @weakify(self);
    _slierView = [HomeSliderView new];
    [_slierView.searchButton addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
        @strongify(self);
        [self pushSearchController];
    }];
    [self.view addSubview:_slierView];
    [_slierView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.left.and.right.mas_equalTo(self.view);
        make.height.mas_equalTo(self.customNavigationHeight);
    }];
}

- (void)pushSearchController
{
    SearchViewController * searchController = [SearchViewController new];
    [self.navigationController pushViewController:searchController animated:YES];
}

-(void)headerViewDidClickCourseWithCourseId:(NSString *)courseId andCuorseName:(NSString *)courseName{
    CourseSortListViewController * controller = [[CourseSortListViewController alloc]init];
    controller.pageTitle = courseName;
    controller.courseTypeId = courseId;
    [self.navigationController pushViewController:controller animated:YES];
}

-(void)headerViewDidClickMoreCourse{
    MoreCourseViewController * moreCourse = [MoreCourseViewController new];
    [self.navigationController pushViewController:moreCourse animated:YES];
}

-(void)tableSectionHeaderViewClickWithTag:(NSInteger)tag
{
    //0 tuijian 1 zuire 2 zuixin
    //1zuixin 2 zuire 3 tuijian
    CourseSortState state;
    if (tag == 0){
        state = CourseSortStataAll;
    }else if (tag == 1)
    {
        state = CourseSortStataHot;
    }else{
        state = CourseSortStataNew;
    }
    CourseSortListViewController * sortController = [[CourseSortListViewController alloc]init];
    sortController.sortState = state;
    [self.navigationController pushViewController:sortController animated:YES];
}

-(void)tableViewDidClickCourseId:(NSString *)courseId
{
    CourseDetailViewController * detailController = [CourseDetailViewController new];
    detailController.courseId = courseId;
    [self.navigationController pushViewController:detailController animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
